﻿Imports System.Windows.Media

Public Class wpfEditor
    'The Friend Controls for this Editor (The WPF Host and the AvalonEditor to be hosted)
    Friend WithEvents iHost As System.Windows.Forms.Integration.ElementHost
    Friend WithEvents iChild As ICSharpCode.AvalonEdit.TextEditor

    'The File Associated With This Editor
    Private FName As String = ""

    ' Does the user have unsaved work?
    Private UnSavedWork As Boolean = False

    ' Property Accessor for Unsaved Work
    Public Property WorkUnsaved As Boolean
        Set(ByVal value As Boolean)
            UnSavedWork = value
        End Set
        Get
            Return UnSavedWork
        End Get
    End Property

    'Property Accessor for FName (Readonly)
    ReadOnly Property FileName As String
        Get
            Return FName
        End Get
    End Property

    Private Sub wpfEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            iChild.Load(FName)
            'Me.rtbEditBox.LoadFile(FName, RichTextBoxStreamType.PlainText)
            UnSavedWork = False
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub

    Public Sub New(ByVal FileToLoadString As String, ByVal ctlNameString As String, Optional ByVal txtFName As String = "")
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        ' Initialize the Host
        iHost = New System.Windows.Forms.Integration.ElementHost
        ' Add the Host to our parent Control
        Me.Controls.Add(iHost)
        ' Initialize the Avalon Editor
        iChild = New ICSharpCode.AvalonEdit.TextEditor
        ' Ensure our Host is Dock-Filled to the Parent
        iHost.Dock = DockStyle.Fill
        ' Set the Child to the Host and we're good to go
        iHost.Child = iChild

        ' Set up a few basic properties, turn on WordWrap by Default
        iChild.WordWrap = True
        ' As Well as LUA SyntaxHighLighting (My Custom Lua Addition)
        iChild.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance.GetDefinition("lua")
        ' And default backcolor
        iChild.Background = New SolidColorBrush(Color.FromRgb(255, 255, 245))
        ' and Set the Font
        iChild.TextArea.FontFamily = New FontFamily((New Uri("pack://application:,,,/Resources/")), "monaco")
        ' New Uri("pack://application:,,,/"), "./Resources/#Consolas")
        'iChild.TextArea.FontFamily = New FontFamily("Consolas")
        iChild.TextArea.FontSize = 11


        ' Set FName to our File
        FName = FileToLoadString
        ' Set the Control Name
        Me.Name = ctlNameString
        ' if we have a txtFName then tag the txtfile
        If txtFName <> "" Then
            Me.Tag = txtFName
        Else
            'else tag the other file
            Me.Tag = FileToLoadString
        End If
    End Sub

     Private Sub iChild_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles iChild.TextChanged
        If UnSavedWork = False Then
            UnSavedWork = True
        End If
        If Me.Parent IsNot Nothing Then
            Me.Parent.Invalidate()
        End If
    End Sub

    Public Sub SaveFile()
        Dim finfo As New System.IO.FileInfo(FName)
        If finfo.Extension = ".rbf" Then
            Me.iChild.Save(FName)
            FileOps.TXTtoRBF(FName, Tag)
        Else
            Me.iChild.Save(FName)
        End If
        UnSavedWork = False
        ' Invalidate to Force Repaint.
        If Me.Parent IsNot Nothing Then
            Me.Parent.Invalidate()
        End If
    End Sub

End Class
